home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLINC.PAK / WSKSOCK.H < prev    next >
C/C++ Source or Header  |  1997-05-06  |  10KB  |  318 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.15  $
  6. //
  7. // Winsock for OWL subsystem.
  8. // Based on work by Paul Pedriana, 70541.3223@compuserve.com
  9. //----------------------------------------------------------------------------
  10. #if !defined(OWL_WSKSOCK_H)
  11. #define OWL_WSKSOCK_H
  12.  
  13. #if !defined(OWL_WINDOW_H)
  14. # include <owl/window.h>
  15. #endif
  16. #if !defined(OWL_WSKADDR_H)
  17. # include <owl/wskaddr.h>
  18. #endif
  19. #if !defined (_WINSOCKAPI_)
  20. # include <winsock.h>
  21. #endif
  22.  
  23. #if defined(BI_NAMESPACE)
  24. namespace OWL {
  25. #endif
  26.  
  27. // Generic definitions/compiler options (eg. alignment) preceeding the 
  28. // definition of classes
  29. #include <services/preclass.h>
  30.  
  31. #define WINSOCK_NOERROR (int)0
  32. #define WINSOCK_ERROR   (int)SOCKET_ERROR
  33.  
  34. #define N_DEF_MAX_READ_BUFFFER_SIZE 8192
  35.  
  36. //
  37. //
  38. //
  39. #define MSG_SOCKET_NOTIFY ((UINT)(WM_USER+301))
  40. #define FD_ALL            (FD_READ|FD_WRITE|FD_OOB|FD_ACCEPT|FD_CONNECT|FD_CLOSE)
  41. #define FD_NONE           (0)
  42.  
  43. //
  44. // Enumeration describing the type of event notifications you want to
  45. // receive for a given socket.
  46. //
  47. enum TNotificationSet {
  48.   NotifyNone    = 0x00,   // No notifications
  49.   NotifyRead    = 0x01,   // Notification of readiness for reading
  50.   NotifyWrite   = 0x02,   // Notification of readiness for writing
  51.   NotifyOOB     = 0x04,   // Notification of the arrival of out-of-band data
  52.   NotifyAccept  = 0x08,   // Notification of incoming connections
  53.   NotifyConnect = 0x10,   // Notification of completed connection
  54.   NotifyClose   = 0x20,   // Notification of socket closure
  55.   NotifyAll     = 0x3F    // All notifications
  56. };
  57.  
  58. //
  59. // Forward ref. 
  60. //
  61. class _OWLCLASS TSocket;
  62.  
  63. //
  64. // class TSocketWindow
  65. // ~~~~~ ~~~~~~~~~~~~~
  66. // A private window used to catch notification messages.
  67. //
  68. class _OWLCLASS TSocketWindow : public TWindow {
  69.   public:
  70.     TSocketWindow(TSocket* socketParent);
  71.     TSocketWindow& operator =(TSocketWindow& src); 
  72.  
  73.     void SetNotificationSet(int notificationSet);
  74.     void SetNotificationWindow(TWindow* windowNotification);
  75.  
  76.     int  GetLastError();
  77.     int  StartAcceptNotification();
  78.     int  StartRegularNotification();
  79.     int  StartCustomNotification(int selectOptions);
  80.     int  CancelNotification();
  81.  
  82.   public_data:
  83.     static uint  MsgSocketNotification; // Message used to notify hwndNotification, if hwndNotification is used.
  84.     TSocket*     SocketParent;          // Exported so Parent can have easy access to it.
  85.  
  86.   protected:
  87.     int           SelectOptions;      // We need to keep our own copy of this so we can do an assignment operator.
  88.     TWindow*      WindowNotification; // A second window that can be notified instead of the Socket.
  89.     int           NotificationSet;    // Types of notification to respond to
  90.     int           LastError;          // Last error
  91.  
  92.     TResult DoNotification(TParam1 param1, TParam2 param2);
  93.  
  94.   DECLARE_RESPONSE_TABLE(TSocketWindow);
  95. };
  96.  
  97. //
  98. // class TSocket
  99. // ~~~~~ ~~~~~~~
  100. // TSocket encapsulates the basic attributes of a socket. A socket is an
  101. // endpoint of communication to which a name may be bound. Each socket in
  102. // use has a type and an associated process.
  103. //
  104. class _OWLCLASS TSocket {
  105.   public:
  106.     // How to shutdown the socket.
  107.     //
  108.     enum TShutMode {
  109.       ShutModeNoRecv = 0,        // No more receives on the socket.
  110.       ShutModeNoSend = 1,        // No more sends on the socket.
  111.       ShutModeNoRecvSend = 2     // No more sends or receives.
  112.     };
  113.  
  114.     TSocket();
  115.     TSocket(SOCKET& newS);
  116.     TSocket(TSocketAddress& newSocketAddress, int nNewFamily = PF_INET,
  117.             int nNewType = SOCK_STREAM, int nNewProtocol = 0);
  118.     virtual ~TSocket();
  119.  
  120.     operator SOCKET() const;
  121.     virtual int CloseSocket();
  122.     virtual int ShutDownSocket(TShutMode shutMode = ShutModeNoRecvSend);
  123.  
  124.     TSocket& operator =(TSocket& newSocket);
  125.     friend bool operator ==(const TSocket& socket1, const TSocket& socket2);
  126.  
  127.     // Commands
  128.     //
  129.     virtual void SetNotificationSet(int notificationSet);
  130.     virtual void SetNotificationWindow(TWindow* windowNotification);
  131.     virtual int  CreateSocket();
  132.     
  133.     // binds to a given address
  134.     //
  135.     virtual int  BindSocket(const TSocketAddress& addressToBindTo);
  136.  
  137.     // binds to our address
  138.     //
  139.     virtual int  BindSocket();
  140.  
  141.     virtual void SetMyAddress(TSocketAddress& newSocketAddress);
  142.     virtual void SetPeerSocketAddress(TSocketAddress& newPeerSocketAddress);
  143.     virtual void SetSocketStyle(int nNewFamily = PF_INET,
  144.                                 int nNewType = SOCK_STREAM,
  145.                                 int nNewProtocol = 0);
  146.     virtual int StartAcceptNotification();
  147.     virtual int StartRegularNotification();
  148.     virtual int StartCustomNotification(int nSelectionOptions);
  149.     virtual int CancelNotification();
  150.  
  151.     // tells class TSocket not to close the socket on deletion
  152.     //
  153.     bool    SetSaveSocketOnDelete(bool saveSocket = true);
  154.  
  155.     virtual int  ConvertProtocol(char* protocol);
  156.  
  157.     // Info
  158.     //
  159.     int GetLastError();
  160.     virtual ulong GetDriverWaitingSize();
  161.     virtual ulong GetTotalWaitingSize();
  162.     virtual int GetMyAddress(TSocketAddress& socketAddress,
  163.                              int& nAddressLength, SOCKET& socket);
  164.     virtual int GetMyAddress(TSocketAddress& socketAddress,
  165.                              int& nAddressLength);
  166.     virtual int GetPeerAddress(TSocketAddress& socketAddress,
  167.                                int& nAddressLength, SOCKET& socket);
  168.     virtual int GetPeerAddress(TSocketAddress& socketAddress,
  169.                                int& nAddressLength);
  170.  
  171.     // Read/Write (Send/Receive)
  172.     //
  173.     virtual void SetMaxReadBufferSize(int nNewMaxReadBufferSize);
  174.  
  175.     // Options
  176.     //
  177.     int SetBroadcastOption(bool bBroadcast);
  178.     int SetDebugOption(bool bDebug);
  179.     int SetLingerOption(bool bLinger, ushort nLingerTime=0);
  180.     int SetRouteOption(bool bRoute);
  181.     int SetKeepAliveOption(bool bKeepAlive);
  182.     int SetOOBOption(bool bSendOOBDataInline);
  183.     int SetReceiveBufferOption(int nReceiveBufferSize);
  184.     int SetSendBufferOption(int nSendBufferSize);
  185.     int SetReuseAddressOption(bool bAllowReuseAddress);
  186.  
  187.     int GetBroadcastOption(bool& bBroadcast);
  188.     int GetDebugOption(bool& bDebug);
  189.     int GetLingerOption(bool& bLinger, ushort& nLingerTime);
  190.     int GetRouteOption(bool& bRoute);
  191.     int GetKeepAliveOption(bool& bKeepAlive);
  192.     int GetOOBOption(bool& bSendOOBDataInline);
  193.     int GetReceiveBufferOption(int& nReceiveBufferSize);
  194.     int GetSendBufferOption(int& nSendBufferSize);
  195.     int GetReuseAddressOption(bool& bAllowReuseAddress);
  196.  
  197.   public_data:
  198.     SOCKET         Handle;             // The Socket handle
  199.     bool           Bound;              // A flag that we can use to tell if
  200.                                        // socket is bound or not.  It gets set
  201.                                        // on automatically, but doesn't get
  202.                                        // set off automatically.
  203.     TSocketAddress SocketAddress;      // My address.
  204.     TSocketAddress PeerSocketAddress;  // Address of Peer to communicate with.
  205.                                        // Used for datagrams and connections.
  206.  
  207.   protected:
  208.     int            Family;             // PF_INET, etc. (this is the protocol family)
  209.     int            Type;               // SOCK_STREAM, etc.
  210.     int            Protocol;           // IPPROTO_TCP, etc.
  211.     int            LastError;          // Last Error.
  212.     int            MaxReadBufferSize;  // Maximum buffer size
  213.     short          SaveSocket;         // Save the socket on deletion?
  214.     TSocketWindow  Window;             // Will receive internal notifications and pass them to this class.
  215.     friend class   TSocketWindow;
  216.  
  217.     // Protected initialization
  218.     //
  219.     void Init();      // Sets up the friend window.
  220.  
  221.     int  SocketsCallCheck(int error);
  222.  
  223.     // Notification
  224.     //
  225.     virtual int DoReadNotification(const SOCKET& s, int nError);
  226.     virtual int DoWriteNotification(const SOCKET& s, int nError);
  227.     virtual int DoOOBNotification(const SOCKET& s, int nError);
  228.     virtual int DoAcceptNotification(const SOCKET& s, int nError);
  229.     virtual int DoConnectNotification(const SOCKET& s, int nError);
  230.     virtual int DoCloseNotification(const SOCKET& s, int nError);
  231. };
  232.  
  233. // Generic definitions/compiler options (eg. alignment) following the 
  234. // definition of classes
  235. #include <services/posclass.h>
  236.  
  237. #if defined(BI_NAMESPACE)
  238. } // namespace OWL
  239. #endif
  240.  
  241. //----------------------------------------------------------------------------
  242. // Inline implementations
  243. //
  244.  
  245. //
  246. // Return the last error on the socket.
  247. //
  248. inline int
  249. TSocketWindow::GetLastError()
  250. {
  251.   return LastError;
  252. }
  253.  
  254. //
  255. // Return the set of notifications socket will catch.
  256. //
  257. inline void
  258. TSocketWindow::SetNotificationSet(int notificationSet)
  259. {
  260.   NotificationSet = notificationSet;
  261. }
  262.  
  263. //
  264. // Set the new notification window.
  265. //
  266. inline void
  267. TSocketWindow::SetNotificationWindow(TWindow* windowNotification)
  268. {
  269.   WindowNotification = windowNotification;
  270. }
  271.  
  272. //
  273. // Assign new set of notifications socket will catch.
  274. //
  275. inline void
  276. TSocket::SetNotificationSet(int notificationSet)
  277. {
  278.   Window.SetNotificationSet(notificationSet);
  279. }
  280.  
  281. //
  282. // Forward method to internal Window.
  283. //
  284. inline void
  285. TSocket::SetNotificationWindow(TWindow* windowNotification)
  286. {
  287.   Window.SetNotificationWindow(windowNotification);
  288. }
  289.  
  290. //
  291. // Return the handle of the socket.
  292. //
  293. inline
  294. TSocket::operator SOCKET() const
  295. {
  296.   return Handle;
  297. }
  298.  
  299. //
  300. // Save the socket on deletion?
  301. //
  302. inline bool
  303. TSocket::SetSaveSocketOnDelete(bool saveSocket)
  304. {
  305.   return SaveSocket = saveSocket;
  306. }
  307.  
  308. //
  309. // Return the last error of socket.
  310. //
  311. inline int
  312. TSocket::GetLastError()
  313. {
  314.   return LastError;
  315. }
  316.  
  317. #endif  // OWL_WSKSOCK_H
  318.